package com.example.wheresmystuff2;

import java.util.ArrayList;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class represents the Sign Up activity, where new users are created.
 * @author Team 26
 */
public class SignUp extends Activity {
	
	//This pattern decides what is a valid email address
	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
			"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
	        "\\@" +
	        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
	        "(" +
	        "\\." +
	        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
	        ")+");
	Database db;
	
	/**
	 * This method checks an email against the valid pattern.
	 * 
	 * @param email The user's entered email
	 * @return True: valid email, False: invalid email
	 */
	public boolean checkEmail(String email) {
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_up);
		
		db = new Database(this);
		db.open();
		
		//References to all of the fields and buttons on screen.
		Button doneButton = (Button) findViewById(R.id.sign_up_done);
		Button cancelButton = (Button) findViewById(R.id.sign_up_cancel);
		final TextView error= (TextView) findViewById(R.id.su_errorField);
		final EditText emailField= (EditText) findViewById(R.id.su_email_field);
		final EditText userField= (EditText) findViewById(R.id.su_user_field);
		final EditText passField= (EditText) findViewById(R.id.su_password_field);
		final EditText retypeField= (EditText) findViewById(R.id.su_rePassword_field);
		
		cancelButton.setOnClickListener(new OnClickListener(){
		     /**
		      * This method cancels the creation of a user and brings you back to Login Activity
		      */
			public void onClick(View v){
				Intent myIntent = new Intent(SignUp.this, LogIn.class);
				SignUp.this.startActivity(myIntent);
			}
		});
		 
	    doneButton.setOnClickListener(new OnClickListener(){
	      /**
	       * This method takes the input and creates a user with it. Then returns you to Login Activity
	       */
	    	public void onClick(View v){
	    		//User's input
	    		String email= emailField.getText().toString();
	    		String username= userField.getText().toString();
	    		String password= passField.getText().toString();
	    		String retype= retypeField.getText().toString();
	    		
	    		//If all fields have input.
	    		if(!(email.equals("") || username.equals("") || password.equals("") || retype.equals(""))){
	    			//If username is at least 4 characters.
	    			if(username.length()>3){
	    				//If email is valid.
	    				if(checkEmail(email)){
	    					//If password was correctly repeated.
	    					if(password.equals(retype)){
	    						db.addUserRow(username, password, email, 3, 0, 0);
	    						ArrayList<Object> users = db.getUserRowAsArray(username);
	    						String s = "";
	    						for(Object o : users){
	    							s += o.toString() + ", ";
	    						}	  
	    						Toast.makeText(getApplicationContext(), "Account created: "+ s, Toast.LENGTH_LONG).show();
	    						
	    						//Return to LogIn Activity
	    						db.close();
	    						Intent myIntent = new Intent(SignUp.this, LogIn.class);
	    						SignUp.this.startActivity(myIntent);
	    					}
	    					else{
	    						error.setText("Passwords do not match.");
	    					}
	    				}
	    				else{
	    					error.setText("Invalid email.");
	    				}
	    			}
	    			else{
	    				error.setText("Username must be at least 4 characters.");  
	    			}
	    		}
	    		else{
	    			error.setText("All fields are required.");
	    		}
	    	}
	    });
	}
}
